/*
* PickupDropoff Module Edit JS
*
* JSLint.com Check:
*/


// functions

RM.Pages.Functions.PickupDropoff_Prices_save = function(){
    var records = RM.Pages.PickupDropoff_Prices_List_Json_Store.getModifiedRecords();
    if(!records.length) {
        return;
    }

    var myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var data = [];
    Ext.each(records, function(r, i) {
        var o = r.getChanges();
        if(r.data.newRecord) {
            o.newRecord = true;
        }
        o.id = r.get('id');
        data.push(o);
    }, this);

    var o = {
         url:RM.Common.AssembleURL({
            controller : 'PickupDropoff',
            action: 'priceupdatejson'
         })
        ,method:'post'
        ,scope:this
        ,params:{
            data: Ext.encode(data)
        }
        ,success: function(responseObject) {
            myMask.hide();
            RM.Pages.PickupDropoff_Prices_List_Json_Store.load();
        },
        failure: function() {
            myMask.hide();
        }
    };
    Ext.Ajax.request(o);
};

RM.Pages.PickupDropoff_Prices_Grid_Create_Row = Ext.data.Record.create([
    {name: 'id', type: 'string'}
]);

RM.Pages.Functions.PickupDropoff_Prices_add = function(){
    var newRow = new RM.Pages.PickupDropoff_Prices_Grid_Create_Row({
        id: 0
    });
    RM.Pages.PickupDropoff_Prices_List_Json_Store.insert(0, newRow);
}

RM.Pages.Functions.PickupDropoff_Prices_delete = function(){
    if (RM.Pages.PickupDropoff_Prices_List_Columns_SM.getSelections().length === 0) {
        Ext.MessageBox.alert(RM.Translate.Common.Error, RM.Translate.Common.NeedToSelectRows);
        return;
    }
    Ext.MessageBox.confirm(RM.Translate.Common.Delete, RM.Translate.Admin.Extras.Edit.DeleteAlert, function(buttonID){
        if (buttonID !== 'yes') {
            return;
        }

        var selections = RM.Pages.PickupDropoff_Prices_List_Columns_SM.getSelections();
        var parametersJson = [];
        var i = 0;for (i; i < selections.length; i++){
            parametersJson[i] = {
                name : 'ids[]',
                value : selections[i].data.id
            };
        }

        var myMask = new Ext.LoadMask('rm_pages_pickupdropoff_prices_mainpage', {msg:RM.Translate.Common.PleaseWait});
        myMask.show();
        var conn = new Ext.data.Connection();
        var request = {
            url: RM.Common.AssembleURL({
                controller : 'PickupDropoff',
                action: 'pricelocationdeletejson',
                parameters : parametersJson
            }),
            method: 'POST',
            success: function(responseObject) {
                RM.Pages.PickupDropoff_Prices_List_Json_Store.load();
                myMask.hide();
            },
            failure: function() {
                Ext.MessageBox.alert(RM.Translate.Common.UnconfirmFailure);
                myMask.hide();
            }
        };
        conn.request(request);
    });
};


// toolbar
RM.Pages.Functions.PickupDropoff_Prices_Toolbar = {
    xtype : "panel",
    id : "rm_pages_pickupdropoff_prices_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([
        {image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Common.Save, link: "RM.Pages.Functions.PickupDropoff_Prices_save()"},
        {image: RM.BaseLargeImageURL+"delete.gif", label: RM.Translate.Common.Delete, link: "RM.Pages.Functions.PickupDropoff_Prices_delete()"},
        {image: RM.BaseLargeImageURL+"new.gif", label: RM.Translate.Common.Add, link: "RM.Pages.Functions.PickupDropoff_Prices_add()"}
    ])
};
RM.Toolbars.push(RM.Pages.Functions.PickupDropoff_Prices_Toolbar);

// main load function
RM.Pages.Functions.PickupDropoff_EditPricesJson = function(){
    Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_pickupdropoff_prices_mainpage');
    RM.Pages.PickupDropoff_Prices_List_Columns_SM.clearSelections();
    Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_pickupdropoff_prices_toolbar');

    Ext.ux.menu.RangeMenu.prototype.icons = {
        gt: 'img/greater_then.png',
        lt: 'img/less_then.png',
        eq: 'img/equals.png'
    };
    Ext.ux.grid.filter.StringFilter.prototype.icon = 'img/find.png';
    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

    RM.Pages.PickupDropoff_Prices_Locations_ArrayStore.load();

    RM.Pages.PickupDropoff_Prices_List_Grid.render();
    RM.Pages.PickupDropoff_Prices_List_Json_Store.load({params:{start: 0, limit: 300}});
}

RM.Pages.PickupDropoff_Prices_List_Json_Store_Fields = [
    {name: "id", type: "numeric"},
    {name: "location_id_a", type: "numeric"},
    {name: "location_id_b", type: "numeric"},
    {name: "price"}
];

RM.Pages.PickupDropoff_Prices_List_Json_Store = new Ext.ux.grid.livegrid.Store({
    url: RM.Common.AssembleURL({
        controller: 'PickupDropoff',
        action: 'pricelistjson'
    }),
    reader: new Ext.ux.grid.livegrid.JsonReader({
            totalProperty: 'total',
            root: 'data'
        },
        RM.Pages.PickupDropoff_Prices_List_Json_Store_Fields
    ),
    id: 'rm_pickupdropoff_prices_list_grid_json_store',
    sortInfo: {field: 'id', direction: 'DESC'},
    bufferSize : 300
});

RM.Pages.PickupDropoff_Prices_List_Columns_SM = new Ext.rm.grid.CheckboxSelectionModel({
    singleSelect: true,
    header: '',
    checkOnly: true
});

RM.Pages.PickupDropoff_Prices_Locations_ArrayStore = new Ext.data.JsonStore({
    fields:[
        {name: "id"},
        {name: "name"}
    ],
    url: RM.Common.AssembleURL({
        controller: 'PickupDropoff',
        action: 'pricelocationsjson'
    })
});

RM.Pages.PickupDropoff_Prices_List_Columns_Rows = [
    RM.Pages.PickupDropoff_Prices_List_Columns_SM,
    {dataIndex: 'id', header: RM.Translate.Common.Id, hidden: false, width: 60},
    {   dataIndex: "location_id_a",
        width: 220,
        header: RM.Translate.Admin.PickupDropoff.Prices.Edit.locationa,
        renderer: function(value, metaData, record, rowIndex, colIndex, store) {
            var i = 0;for (i; i < RM.Pages.PickupDropoff_Prices_Locations_ArrayStore.data.items.length; i++){
                if (RM.Pages.PickupDropoff_Prices_Locations_ArrayStore.data.items[i].data.id === value) {return RM.Pages.PickupDropoff_Prices_Locations_ArrayStore.data.items[i].data.name;}
            }
            return value;
        },
        editor: new Ext.form.ComboBox({
            id: 'rm_pickupdropoff_prices_locationa_combo',
            hiddenName: 'rm_pickupdropoff_prices_locationa_combo_hidden',
            mode: 'local',
            displayField: "name",
            valueField: "id",
            triggerAction: 'all',
            typeAhead: true,
            resizable: true,
            minListWidth: 220,
            store: RM.Pages.PickupDropoff_Prices_Locations_ArrayStore
        })
    },
    {   dataIndex: "location_id_b",
        width: 220,
        header: RM.Translate.Admin.PickupDropoff.Prices.Edit.locationb,
        renderer: function(value, metaData, record, rowIndex, colIndex, store) {
            var i = 0;for (i; i < RM.Pages.PickupDropoff_Prices_Locations_ArrayStore.data.items.length; i++){
                if (RM.Pages.PickupDropoff_Prices_Locations_ArrayStore.data.items[i].data.id === value) {return RM.Pages.PickupDropoff_Prices_Locations_ArrayStore.data.items[i].data.name;}
            }
            return value;
        },
        editor: new Ext.form.ComboBox({
            id: 'rm_pickupdropoff_prices_locationb_combo',
            hiddenName: 'rm_pickupdropoff_prices_locationb_combo_hidden',
            mode: 'local',
            displayField: "name",
            valueField: "id",
            triggerAction: 'all',
            typeAhead: true,
            resizable: true,
            minListWidth: 220,
            store: RM.Pages.PickupDropoff_Prices_Locations_ArrayStore
        })
    },
    {
        dataIndex: 'price',
        header: RM.Translate.Admin.PickupDropoff.Prices.Edit.price,
        editor: new Ext.form.NumberField({
            allowBlank: false,
            allowNegative: false
        })
    }
];

RM.Pages.PickupDropoff_Prices_List_Columns = new Ext.grid.ColumnModel({
    columns: RM.Pages.PickupDropoff_Prices_List_Columns_Rows,
    defaults: {
        sortable: true
    }
});
RM.Pages.PickupDropoff_Prices_List_Filters = new Ext.ux.grid.GridFilters({
    filters: [
        {dataIndex: 'id', type: 'numeric'},
        {dataIndex: 'location_id_a', type: 'numeric'},
        {dataIndex: 'location_id_b', type: 'numeric'},
        {dataIndex: 'price', type: 'string'}
    ]
});

RM.Pages.PickupDropoff_Prices_List_Grid_View = new Ext.ux.grid.livegrid.GridView({
    nearLimit : 100,
    loadMask  : {
        msg :  'Buffering. Please wait...'
    }
});

RM.Pages.PickupDropoff_Prices_List_Grid = new Ext.ux.grid.livegrid.EditorGridPanel({
    id : 'rm_pickupdropoff_prices_list_grid',
    clicksToEdit: 1,
    plugins: RM.Pages.PickupDropoff_Prices_List_Filters,
    bbar     : new Ext.ux.grid.livegrid.Toolbar({
            view        : RM.Pages.PickupDropoff_Prices_List_Grid_View,
            displayInfo : true
    }),
    enableColLock : false,
    loadMask : {
        msg: RM.Translate.Common.PleaseWait
    },
    height : RM.Common.GetPanelHeight(106),
    cm : RM.Pages.PickupDropoff_Prices_List_Columns,
    store : RM.Pages.PickupDropoff_Prices_List_Json_Store,
    selModel : RM.Pages.PickupDropoff_Prices_List_Columns_SM,
    view: RM.Pages.PickupDropoff_Prices_List_Grid_View,
    viewConfig: {
        forceFit: true
    }
});

// main function
RM.Pages.PickupDropoff_Prices_MainPanel = new Ext.Panel({
    id : 'rm_pages_pickupdropoff_prices_mainpage',
    title : RM.Translate.Common.List,
    iconCls: "RM_pickupdropoff_root_icon",
    items : [
        RM.Pages.PickupDropoff_Prices_List_Grid
    ]
});

RM.Main.Pages.push(RM.Pages.PickupDropoff_Prices_MainPanel);